/**
 * File Name   : MenuCfgAction.java
 * Description : 
 */
package com.hh.admin.menu.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.menu.form.MenuCfgForm;
import com.hh.admin.menu.form.ResultMenuCfgBean;
import com.hh.admin.menu.service.MenuCfgService;
import com.hh.common.consts.AdminConst;

/**
 * 菜单管理
 * 
 * @author 冯昌硕
 * @create 2012-12-26 下午9:37:14
 */
public class MenuCfgAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(MenuCfgAction.class);
    
    private MenuCfgService menuCfgService;
    
    /**
     * 获取功能菜单信息列表
     * @create 2012-12-26 下午9:52:44
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MenuCfgForm menuForm = (MenuCfgForm) form;
        
        // 查询当前页要显示的记录
        List<ResultMenuCfgBean> menuLst = menuCfgService.getMenuList(menuForm);
        request.setAttribute("menuLst", menuLst);
        
        // 查询总记录数
        int totleCnt = menuCfgService.getMenuTotleCnt(menuForm);
        menuForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / menuForm.getPageSize();
        if(totleCnt % menuForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        menuForm.setTotlePage(pageCnt);
        log.debug("查询菜单记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        String result = null;
        int menuType = menuForm.getMenuType();
        if(menuType == AdminConst.MENU_TYPE_FUN) {
            result = "fun_list";
        } else if(menuType == AdminConst.MENU_TYPE_EXP) {
            result = "exp_list";
        }
        
        log.debug("查询菜单列表menuType=[" + menuType + "] result=[" + result + "]");
        return mapping.findForward(result);
    }
    
    /**
     * 删除单条菜单记录
     * @create 2012-12-26 下午11:47:13
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MenuCfgForm menuForm = (MenuCfgForm) form;
        
        int id = menuForm.getId();
        log.debug("功能菜单项删除，要删除的菜单项ID=[" + id + "]");
        int delCnt = menuCfgService.deleteOneMenu(id);
        log.debug("功能菜单项[" + id + "]删除，删除影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2012-12-27 上午12:01:31
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MenuCfgForm menuForm = (MenuCfgForm) form;
        Integer[] ids = menuForm.getIds();
        
        int delCnt = menuCfgService.deleteManyMenu(ids);
        log.debug("批量删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 打开菜单详情页进行内容编辑
     * @create 2012-12-27 下午7:30:02
     */
    public ActionForward detail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MenuCfgForm menuForm = (MenuCfgForm) form;
        int id = menuForm.getId();
        ResultMenuCfgBean result = menuCfgService.getMenuDetail(id);
        
        request.setAttribute("menuDetail", result);
        
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的菜单内容更新
     * @create 2012-12-27 下午7:52:43
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MenuCfgForm menuForm = (MenuCfgForm) form;
        int updCnt = menuCfgService.updateMenu(menuForm);
        log.debug("修改菜单[" + menuForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        return mapping.findForward("success");
    }
    
    /**
     * 添加菜单记录
     * @create 2012-12-27 下午8:44:14
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MenuCfgForm menuForm = (MenuCfgForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        menuForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = menuCfgService.insertMenu(menuForm);
        log.debug("新增菜单ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }

    public void setMenuCfgService(MenuCfgService menuCfgService) {
        this.menuCfgService = menuCfgService;
    }
    
}
